package cn.edu.njupt.post.controller;

import cn.edu.njupt.post.async.EventModel;
import cn.edu.njupt.post.async.EventProducer;
import cn.edu.njupt.post.async.EventType;
import cn.edu.njupt.post.model.Comment;
import cn.edu.njupt.post.model.EntityType;
import cn.edu.njupt.post.model.HostHolder;
import cn.edu.njupt.post.model.User;
import cn.edu.njupt.post.service.CommentService;
import cn.edu.njupt.post.service.PostService;
import cn.edu.njupt.post.service.UserService;
import cn.edu.njupt.post.util.OtherUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import java.util.Date;

@Controller
@RequestMapping({"/comment"})
public class CommentController {
    private static final Logger logger = LoggerFactory.getLogger(FeedController.class);

    @Autowired
    HostHolder hostHolder;

    @Autowired
    UserService userService;

    @Autowired
    CommentService commentService;

    @Autowired
    PostService postService;

    @Autowired
    EventProducer eventProducer;

    @RequestMapping(path = {"/add"}, method = {RequestMethod.POST})
    public String addComment(@RequestParam("entity_id") int entityId,
                             @RequestParam("entity_type") int entityType,
                             @RequestParam("post_id") int postId,
                             @RequestParam("content") String content) {
        try {

            if (hostHolder.getUser() != null) {
                Comment comment = new Comment();
                comment.setUserId(hostHolder.getUser().getId())
                        .setContent(HtmlUtils.htmlEscape(content))
                        .setEntityId(entityId)
                        .setEntityType(entityType)
                        .setPostId(postId)
                        .setCreatedDate(new Date())
                        .setStatus(0);

                commentService.addComment(comment);
                // 更新题目里的评论数量
                int count = commentService.getCommentCount(postId);
                postService.updateCommentCount(postId, count);
                // 怎么异步化
                eventProducer.fireEvent(new EventModel(EventType.COMMENT).setActorId(comment.getUserId())
                        .setEntityId(postId));
            }
        } catch (Exception e) {
            logger.error("增加评论失败" + e.getMessage());
        }
        return "redirect:/post/" + String.valueOf(postId);
    }

    @RequestMapping(value = "/remove", method = {RequestMethod.POST})
    @ResponseBody
    public String removePost(@RequestParam("id") int id,
                             @RequestParam("post_id") int postId) {
        commentService.deleteComment(id);
        int count = commentService.getCommentCount(postId);
        postService.updateCommentCount(postId, count);
        return OtherUtil.getJSONString(0, "ok");
    }
}
